package net.kazed.nextaction.tools;

import java.security.ProviderException;
import java.util.Date;

import net.kazed.nextaction.R;
import net.kazed.nextaction.database.GtdProvider;
import net.kazed.nextaction.database.serialize.BackupFile;
import net.kazed.nextaction.database.serialize.BackupFileSummary;
import net.kazed.nextaction.database.serialize.BackupSummaryExternalCursor;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

/**
 * Display list of backup files on the external storage.
 */
public class BackupImportActivity extends ListActivity {

    public static final String SELECTED_ITEM = "SELECTED_ITEM";

    private static final String TAG = "BackupImportActivity";
    
    private Cursor cursor;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.backup_import_dialog);
        setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);
        determineBackupFiles();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void determineBackupFiles() {
        cursor = managedQuery(BackupFile.CONTENT_EXTERNAL_URI, null, null, null, null);
        setListAdapter(new BackupListAdapter(this, R.layout.list_item_view, cursor));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       determineBackupFiles();
       onContentChanged();
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Log.d(TAG, "onListItemClick" + position);
        if (cursor.moveToPosition(position)) {
            long created = cursor.getLong(BackupSummaryExternalCursor.INDEX_ID);
            Uri itemUri = ContentUris.withAppendedId(BackupFile.CONTENT_EXTERNAL_URI, created);
            try {
                getContentResolver().update(itemUri, null, GtdProvider.BACKUP_IMPORT_FROM_SD_CARD, null);
                Intent result = new Intent(Intent.ACTION_PICK, itemUri);
                setResult(RESULT_OK, result);
                Toast toast = Toast.makeText(BackupImportActivity.this, R.string.backup_import_successful, Toast.LENGTH_LONG);
                toast.show();
                finish();
            } catch (ProviderException e) {
                Log.e(TAG, "Import failed", e);
                Toast toast = Toast.makeText(BackupImportActivity.this, R.string.backup_import_failure, Toast.LENGTH_LONG);
                toast.show();
            }
        }
    }
    
    private class BackupListAdapter extends SimpleCursorAdapter {
        
        private static final String cTag = "TaskListAdapter";

        public BackupListAdapter(Context context, int listItemLayoutId, Cursor cursor) {
           super(context, listItemLayoutId, cursor, new String[] { BackupFileSummary.COLUMN_ID },
                    new int[] { R.id.name });
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           BackupListItemView backupListItemView = null;
           if (convertView instanceof BackupListItemView) {
               backupListItemView = (BackupListItemView) convertView;
           } else {
               backupListItemView = new BackupListItemView(parent.getContext(), false);
           }
           Cursor cursor = getCursor();
           if (cursor.moveToPosition(position)) {
               long created = getCursor().getLong(0);
               BackupFileSummary backupFileSummary = new BackupFileSummary(new Date(created), 0, 0);
               backupListItemView.updateView(backupFileSummary);
           }
           return backupListItemView;
        }

     }

}